package com.rivulet.ncards;


import android.app.AlertDialog;
import android.content.pm.ApplicationInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.rivulet.ncards.gameRules.StringList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class CardGameActivity extends AppCompatActivity {
    private static final int COMPLETED = 0;
    Button back,end_round,battle_time;
    Button enemy_deck,your_deck,enemy_graveyard,your_graveyard;
    ImageButton enemy_area10;
    StringList enemy_cards=new StringList(6);
    StringList your_cards=new StringList(6);
    StringList your_areas=new StringList(10);
    StringList enemy_areas=new StringList(10);
    Integer enemy_decks,enemy_graveyards,your_graveyards;
    Integer round,first_round;
    Integer round_card_can_get=0;
    Integer battle_round=0;
    List<String> your_decks= new ArrayList<>(Arrays.asList(new String[]{"xkly", "kz", "ss", "llzl", "hsb", "cmly", "lrj"}));//牌组
    Integer your_hand_card=0;//你的手牌
    Integer enemy_hand_card=0;//敌人的手牌
    /*
    * first_round=1时代表you是先手，enemy后手，反之..
    * round记录当前回合数
    * round%2==1代表是you的回合，反之..
    */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_game);
        //回合初始化，以及锁定这是我方先攻
        round=1;
        first_round=1;

        ImageView[] views={
                (ImageView) findViewById(R.id.your_card1),
                (ImageView) findViewById(R.id.your_card2),
                (ImageView) findViewById(R.id.your_card3),
                (ImageView) findViewById(R.id.your_card4),
                (ImageView) findViewById(R.id.your_card5),
                (ImageView) findViewById(R.id.your_card6),
                (ImageView) findViewById(R.id.enemy_card1),
                (ImageView) findViewById(R.id.enemy_card2),
                (ImageView) findViewById(R.id.enemy_card3),
                (ImageView) findViewById(R.id.enemy_card4),
                (ImageView) findViewById(R.id.enemy_card5),
                (ImageView) findViewById(R.id.enemy_card6),
        };
        back=(Button) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        battle_time= (Button) findViewById(R.id.battle_time);
        battle_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(round%2==first_round){
                    AlertDialog msg = new AlertDialog.Builder(CardGameActivity.this)
                            .setTitle("战斗阶段")
                            .setMessage("在战斗阶段中，你可以选择一张人物卡召唤到场地上，任何区域都可以！")
                            .create();
                    msg.show();
                }else {
                    AlertDialog msg = new AlertDialog.Builder(CardGameActivity.this)
                            .setMessage("现在不是你的回合！")
                            .create();
                    msg.show();
                }
            }
        });
        end_round=(Button) findViewById(R.id.end_round);

        end_round.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(round%2==first_round){
                    round+=1;
                    round_card_can_get=0;
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            if(enemy_hand_card<6){
                                enemy_hand_card+=1;
                                Message message=new Message();
                                message.what=COMPLETED;
                                handler.sendMessage(message);
                                round_card_can_get++;
                                try {
                                    Thread.sleep(10000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                round+=1;
                                Message turn_message=new Message();
                                turn_message.what=1;
                                your_turn.sendMessage(turn_message);
                            }
                        }
                    }).start();
                }else {
                    AlertDialog msg=new AlertDialog.Builder(CardGameActivity.this)
                            .setMessage("现在不是你的回合，你不能结束！")
                            .create();
                    msg.show();
                }
            }
        });
        enemy_deck=(Button) findViewById(R.id.enemy_deck);
        enemy_graveyard=(Button)findViewById(R.id.enemy_graveyard);
        your_deck=(Button) findViewById(R.id.your_deck);
        your_graveyard=(Button)findViewById(R.id.your_graveyard);
        your_deck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(round%2==first_round){
                    if (round_card_can_get==0){
                        if(your_hand_card>=6){
                            AlertDialog msg=new AlertDialog.Builder(CardGameActivity.this)
                                    .setMessage("你的手牌已到达上限6张，你暂时无法抽牌！")
                                    .create();
                            msg.show();
                        }else {
                            your_hand_card+=1;
                            ImageView card = null;
                            switch (your_hand_card){
                                case 1:card=(ImageView) findViewById(R.id.your_card1);break;
                                case 2:card=(ImageView) findViewById(R.id.your_card2);break;
                                case 3:card=(ImageView) findViewById(R.id.your_card3);break;
                                case 4:card=(ImageView) findViewById(R.id.your_card4);break;
                                case 5:card=(ImageView) findViewById(R.id.your_card5);break;
                                case 6:card=(ImageView) findViewById(R.id.your_card6);break;
                                default:break;
                            }
                            int index=new Random().nextInt(your_decks.size());
                            card.setImageBitmap(getRes(your_decks.get(index)));
                            your_decks.remove(index);
                            round_card_can_get++;
                        }
                    }else {
                        AlertDialog msg=new AlertDialog.Builder(CardGameActivity.this)
                                .setMessage("一回合只能抽一张牌！")
                                .create();
                        msg.show();
                    }
                }
                else {
                    AlertDialog msg=new AlertDialog.Builder(CardGameActivity.this)
                            .setMessage("现在不是你的回合！")
                            .create();
                    msg.show();
                }
            }
        });

        enemy_area10=(ImageButton) findViewById(R.id.enemy_area10);
        enemy_area10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder show_card = new AlertDialog.Builder(CardGameActivity.this);
                ImageView view=new ImageView(CardGameActivity.this);
                view.setImageResource(R.drawable.kz);
                show_card.setView(view);
                show_card.create().show();
            }
        });

        for (ImageView view:views){
            view.setOnClickListener(imageViewListener);
        }
    }

    /**
     * 因为安卓不允许子线程调用ui线程的视图，所以需要一个回调函数
     */
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == COMPLETED){
                ImageView card = null;
                switch (your_hand_card){
                    case 1:card=(ImageView) findViewById(R.id.enemy_card1);break;
                    case 2:card=(ImageView) findViewById(R.id.enemy_card2);break;
                    case 3:card=(ImageView) findViewById(R.id.enemy_card3);break;
                    case 4:card=(ImageView) findViewById(R.id.enemy_card4);break;
                    case 5:card=(ImageView) findViewById(R.id.enemy_card5);break;
                    case 6:card=(ImageView) findViewById(R.id.enemy_card6);break;
                    default:break;
                }
                card.setImageResource(R.drawable.back);
            }
        }
    };
    private Handler your_turn=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 1){
                AlertDialog show=new AlertDialog.Builder(CardGameActivity.this)
                        .setMessage("对方结束回合，现在轮到你行动了")
                        .create();
                show.show();
            }
        }
    };
    //手牌及场地监听
    private View.OnClickListener imageViewListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder show=new AlertDialog.Builder(CardGameActivity.this);
            ImageView imageView =(ImageView) findViewById(v.getId());
            Drawable drawable = imageView.getDrawable();
            ImageView view=new ImageView(CardGameActivity.this);
            view.setImageDrawable(drawable);
            show.setView(view);
            show.show();
        }
    };
    //获取Res下的drawable文件夹下图片资源
    private Bitmap getRes(String imageName) {
        ApplicationInfo appInfo = this.getApplicationInfo();
        int resID = getResources().getIdentifier(imageName, "drawable", appInfo.packageName);
        return BitmapFactory.decodeResource(getResources(), resID);
    }
}
